﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CMS.Message;
using TRPOP.CMS.Message.model;
using TRPOP.CMS.Message.UI;
using TRPOP.Common;
using TRPOP.CMS.Web.UI;

namespace TRPOP.CMS.Web.cms.Message
{
    public partial class mseeage_save : BaseAdmin
    {
        protected IMessage m_IMessage = MessageAccess.GetInstance();
        protected int intMessageId = 0;
        MessageInfo oInfo = null;

        #region 页面事件
        
        protected void Page_Load(object sender, EventArgs e)
        {
           
            intMessageId = RequestHelper.RequestInt("id");
            if (!IsPostBack)
            {
                if (intMessageId > 0)
                {
                    BindData();
                    BindType();
                    BindStatus();
                }
            }
        }

        //保存留言
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (intMessageId > 0)
            {
                oInfo = m_IMessage.GetMessageInfo(intMessageId);

                if (String.IsNullOrEmpty(txtMessageTitle.Text) || ddlType.SelectedValue.ToInt() <= 0 || ddlStatus.SelectedValue.ToInt() <= 0)
                {
                    Alert("留言标题、状态、类型不能为空");
                    return;
                }

                oInfo.nvcMessageTitle = txtMessageTitle.Text.Trim();
                oInfo.ntMessageContent = txtMessageContent.Text.Trim();
                oInfo.iMessageSend = txtMesageSendID.Text.ToInt();
                oInfo.nvcMessageSend = txtMesageSendName.Text.Trim();
                oInfo.nvcContactName = txtContactName.Text.Trim();
                oInfo.nvcContactEmail = txtEmail.Text.Trim();
                oInfo.nvcContactMobile = txtMobile.Text.Trim();
                oInfo.nvcContactPhone = txtPhone.Text.Trim();
                oInfo.nvcContactPostCode = txtZipcode.Text.Trim();
                oInfo.nvcContactAddress = txtAddress.Text.Trim();
                oInfo.iMessageTypeID = ddlType.SelectedValue.ToInt();
                oInfo.iMessageStatusID = ddlStatus.SelectedValue.ToInt();
                oInfo.iContactGender = rbtlGender.SelectedValue.ToInt();

                if (m_IMessage.SaveMessage(oInfo))
                {
                    Alert("保存成功！", "message_list.aspx");

                }
                else
                {
                    Alert("保存失败！");
                }
            }
        }

        #endregion

        #region 页面方法
        
        //获取留言信息
        protected void BindData()
        {
            oInfo = m_IMessage.GetMessageInfo(intMessageId);

            if (oInfo != null)
            {
                txtMessageTitle.Text = oInfo.nvcMessageTitle;
                txtMessageContent.Text = oInfo.ntMessageContent;

                if (oInfo.iMessageSend <= 0)
                {
                    txtMesageSendID.Text = "";
                }
                else
                { 
                txtMesageSendID.Text = oInfo.iMessageSend.ToString();
                }
                
                txtMesageSendName.Text = oInfo.nvcMessageSend;
                txtContactName.Text = oInfo.nvcContactName;
                txtEmail.Text = oInfo.nvcContactEmail;
                txtMobile.Text = oInfo.nvcContactMobile;
                txtPhone.Text = oInfo.nvcContactPhone;
                txtZipcode.Text = oInfo.nvcContactPostCode;
                txtAddress.Text = oInfo.nvcContactAddress;
                ddlType.SelectedValue = oInfo.iMessageTypeID.ToString();
                ddlStatus.SelectedValue = oInfo.iMessageStatusID.ToString();
                rbtlGender.SelectedValue=oInfo.iContactGender.ToString();
            }
        }

        //绑定留言类型
        protected void BindType()
        {
            ddlType.DataSource = m_IMessage.GetMessageTypeInfoList(0, "", "");
            ddlType.DataTextField = "nvcMessageTypeName";
            ddlType.DataValueField = "iMessageTypeID";
            ddlType.DataBind();
            ddlType.Items.Insert(0, new ListItem("请选择类型", "0"));
        }

        //绑定留言状态
        protected void BindStatus()
        {
            ddlStatus.DataSource = m_IMessage.GetMessageStatusList(0, "", "");
            ddlStatus.DataTextField = "nvcMessageStatusName";
            ddlStatus.DataValueField = "iMessageStatusID";
            ddlStatus.DataBind();
            ddlStatus.Items.Insert(0, new ListItem("请选择状态", "0"));
        }
        #endregion
    }
}